﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;

namespace RMS
{
    public partial class frmManagePassword : ICommonForm
    {
        public frmManagePassword()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Utilities.VerifyAdmin(txtPassOld.Text))
            {
                if (txtPassNew1.Text != txtPassNew2.Text)
                {
                    MessageBox.Show(Constants.WrongNewPassMess);
                    txtPassNew2.Clear();
                    txtPassNew1.Clear();
                }
                else
                    if (txtPassNew1.Text == "")
                        MessageBox.Show(Constants.RequiredField);
                    else
                        ChangePassword();
            }
            else
            {
                MessageBox.Show(Constants.WrongOldPassMess);
                txtPassOld.Clear();
            }
        }

        private void ChangePassword()
        {
            string code = System.IO.File.ReadAllText(Constants.VerifyAdminPath);
            var file = new System.IO.StreamWriter(Constants.VerifyAdminPath);
            try
            {
                var arr = code.Split('#');
                file.Write(Utilities.Encode(txtPassNew1.Text) + "#" + arr[1] + "#" + arr[2] + "#" + arr[3]);
                MessageBox.Show(Constants.ChangePassOK);
                txtPassNew1.Clear();
                txtPassNew2.Clear();
                txtPassOld.Clear();
            }
            finally
            {
                file.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmManagePassword_Load(object sender, EventArgs e)
        {
        }
    }
}
